﻿using CasignDesignGA.Casign;
using CasignDesignGA.Casing;
using CasignDesingGA.Casing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CasignDesignGA.Engine.Mutation
{
    class RandomMutationAlgorithm : IMutationAlgorithm
    {
        private static RandomMutationAlgorithm instance;
        private static double FIRSTCASINGMUTATIONTHRESHOLD = 0.5;
        private RandomMutationAlgorithm() { }

        public static RandomMutationAlgorithm Instance
        {
           get 
           {
              if (instance == null)
               {
                   instance = new RandomMutationAlgorithm();
               }
               return instance;
             }
         }


        public ISelectable mutate(ISelectable individual)
        {
            Random randomGenerator = new Random(DateTime.Today.Millisecond);
            double ran = randomGenerator.NextDouble();
            CasingUnit toBeMutated;
            int mutatingIndex = 0;
            
            if (ran > FIRSTCASINGMUTATIONTHRESHOLD)
            {
                mutatingIndex = 1; 
            }

            toBeMutated = ((CasingDesign)individual).CasingCollection[mutatingIndex];

            CasingUnit randomCasingFromCatalog = CasingCatalog.Instance.Catalog[randomGenerator.Next(CasingCatalog.Instance.Catalog.Count)];

            toBeMutated.Axial = randomCasingFromCatalog.Axial;
            toBeMutated.Burst = randomCasingFromCatalog.Burst;
            toBeMutated.Collapse = randomCasingFromCatalog.Collapse;
            toBeMutated.Drift = randomCasingFromCatalog.Drift;
            toBeMutated.Id = randomCasingFromCatalog.Id;
            toBeMutated.MetricWeight = randomCasingFromCatalog.MetricWeight;
            toBeMutated.Od = randomCasingFromCatalog.Od;
            toBeMutated.Price = randomCasingFromCatalog.Price;
            toBeMutated.SteelGrade = randomCasingFromCatalog.SteelGrade;
            toBeMutated.Uts = randomCasingFromCatalog.Uts;
            toBeMutated.WallThickness = randomCasingFromCatalog.WallThickness;
            toBeMutated.Ys = randomCasingFromCatalog.Ys;

            return individual;


        }
    }
}
